#!/bin/bash

set -ux

# isula root  dir
isula_root=/home/isula
cni_version=1.2.0

# echo '修改ssh配置'
# sed -i -e '/^#UseDNS/c UseDNS no' \
#   -e '/^GSSAPIAuthentication/c GSSAPIAuthentication no' \
#   /etc/ssh/sshd_config &&
#   systemctl restart sshd

echo '安装iSulad'
dnf install -y iSulad isula-build jq tar &&
  dnf clean all

echo '安装cni'
mkdir -p /opt/cni/bin/ &&
  curl -Lo cni-plugins-linux-amd64-v$cni_version.tgz \
    https://ghproxy.com/https://github.com/containernetworking/plugins/releases/download/v$cni_version/cni-plugins-linux-amd64-v$cni_version.tgz &&
  tar -zxvf cni-plugins-linux-amd64-v$cni_version.tgz -C /opt/cni/bin/

echo '设置iSulad'

# 备份配置文件
cp /etc/isulad/daemon.json /etc/isulad/daemon.json.origin

# 创建目录
mkdir -p $isula_root/{lib,run}/isula

# 修改配置
cp /etc/isulad/daemon.json /etc/isulad/daemon.json.tmp

sed -i -e "s#/var/lib/isula#/$isula_root/lib/isula#g" \
  -e "s#/var/run/isula#/$isula_root/run/isula#g" \
  -e '/registry-mirrors/a "docker.io",' \
  -e '/registry-mirrors/a "https://3otatncp.mirror.aliyuncs.com",' \
  -e '/registry-mirrors/a "https://ac8965cb96a04f8e864a71203f1e5f2c.mirror.swr.myhuaweicloud.com",' \
  -e '/registry-mirrors/a "http://hub-mirror.c.163.com"' \
  -e '/network-plugin/c "network-plugin": "cni",' \
  /etc/isulad/daemon.json.tmp

# 将tmp 复制到 daemon.json
cat /etc/isulad/daemon.json.tmp | jq | cat >/etc/isulad/daemon.json &&
  rm -rf /etc/isulad/daemon.json.tmp

# 查看配置
cat /etc/isulad/daemon.json | jq

# 重启服务
systemctl restart isulad && systemctl status isulad

echo "查看开放的端口"
firewall-cmd --zone=public --list-ports

echo '查看容器'
isula info
